﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Collections.Generic;

namespace XCI.Data.Entity
{
    /// <summary>
    /// 实体信息
    /// </summary>
    public class EntityData
    {
        /// <summary>
        /// 实体类型
        /// </summary>
        public Type EntityType { get; internal set; }

        /// <summary>
        /// 表特性
        /// </summary>
        public TableAttribute TableAttr { get; internal set; }

        /// <summary>
        /// 主键特性
        /// </summary>
        public PrimaryAttribute PrimaryAttr { get; internal set; }

        /// <summary>
        /// 主键列
        /// </summary>
        public EntityColumn PrimaryProperty { get; internal set; }

        /// <summary>
        /// 上级主键列
        /// </summary>
        public EntityColumn ParentProperty { get; internal set; }

        /// <summary>
        /// 排序列
        /// </summary>
        public EntityColumn PathProperty { get; internal set; }

        /// <summary>
        /// 查询列集合
        /// </summary>
        public Dictionary<string, EntityColumn> DbColumns { get; internal set; }

        /// <summary>
        /// 数据库忽略列集合
        /// </summary>
        public Dictionary<string, EntityColumn> IgnoreColumns { get; internal set; }

        /// <summary>
        /// 所有列信息
        /// </summary>
        public Dictionary<string, EntityColumn> AllColumns { get; internal set; }

        /// <summary>
        /// 获取主键值
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>返回实体主键值</returns>
        public object GetPrimaryValue(object entity)
        {
            return PrimaryProperty?.GetValue(entity);
        }

        /// <summary>
        /// 获取主键值
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="pkValue">主键值</param>
        /// <returns>返回实体主键值</returns>
        public void SetPrimaryValue(object entity, object pkValue)
        {
            PrimaryProperty?.SetValue(entity, pkValue);
        }
    }
}
